package com.huan.vertxle.verticle;

import com.huan.vertxle.initialization.StartParamsInit;
import com.huan.vertxle.model.ConfigParams;
import com.huan.vertxle.router.WebRouter;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;

/**
 * web服务启动verticle
 *
 * @author huan
 */
public class WebRouterVerticle extends AbstractVerticle {
  private final InternalLogger logger = Slf4JLoggerFactory.getInstance(this.getClass());

  /**
   * 初始化服务
   *
   * @throws Exception
   */
  @Override
  public void start() throws Exception {
    HttpServer server = vertx.createHttpServer();
    WebRouter webRouter = new WebRouter();
    ConfigParams.ServiceDTO service = StartParamsInit.init.getService();
    Router router = webRouter.router;
    server.requestHandler(router).listen(service.getPort()).onSuccess(event -> {
      logger.info("Vert.x 启动,端口:{}", service.getPort());
    }).onFailure(event -> {
      logger.info("服务关闭:{}", event.getMessage());
      server.close();
    });
  }
}
